package com.jdls.tanhua.server;

import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.jdls.tanhua.dubbo.UserInfoApi;
import com.jdls.tanhua.dubbo.VoiceApi;
import com.jdls.tanhua.exception.AllException;
import com.jdls.tanhua.interceptor.UserHolder;
import com.jdls.tanhua.mongo.Voice;
import com.jdls.tanhua.pojo.UserInfo;
import com.jdls.tanhua.vo.ErrorResult;
import com.jdls.tanhua.vo.VoiceVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @author 嘉德罗斯
 * @date 2021/11/8
 */
@Service
public class VoiceServer {

    @DubboReference
    private VoiceApi voiceApi;

    @Autowired
    private FastFileStorageClient client;

    @Autowired
    private FdfsWebServer webServer;

    @DubboReference
    private UserInfoApi userInfoApi;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    //上传语音
    public void upVoice(MultipartFile soundFile) throws IOException {

        //语音上传
        //获取文件的后缀名
        String filename = soundFile.getOriginalFilename();  //ssss.avi
        String sufix = filename.substring(filename.lastIndexOf(".")+1);
        StorePath storePath = client.uploadFile(soundFile.getInputStream(), soundFile.getSize(), sufix, null);
        //获取语音地址
        String voiceUrl = webServer.getWebServerUrl() + storePath.getFullPath();


        //构造对象
        Long userId = UserHolder.getUserId();
        Voice voice = new Voice();
        voice.setUserId(userId);
        voice.setCreated(System.currentTimeMillis());
        voice.setVoiceUrl(voiceUrl);
        voiceApi.save(voice);

    }


    //查看语音
    public VoiceVo find() {
        Long userId = UserHolder.getUserId();
        String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String key = "voice"+format+userId;

        String value = redisTemplate.opsForValue().get(key);
        if ("0".equals(value)){
            throw new AllException(ErrorResult.upErr());
        }
        Voice voice = voiceApi.find(userId);

        if (voice == null){
            throw new AllException(ErrorResult.voiceErr());
        }

        UserInfo userInfo = userInfoApi.find(voice.getUserId());

        VoiceVo voiceVo = new VoiceVo();
        voiceVo.setSoundUrl(voice.getVoiceUrl());
        voiceVo.setNickname(userInfo.getNickname());
        voiceVo.setAvatar(userInfo.getAvatar());
        voiceVo.setAge(Integer.valueOf(userInfo.getAge()));
        voiceVo.setGender(userInfo.getGender());

        if (value == null){
            value = 10+"";
        }
        Integer a  = Integer.valueOf(value)-1;

        voiceVo.setRemainingTimes(a);
        redisTemplate.opsForValue().set(key,a+"",1L,TimeUnit.DAYS);
        client.deleteFile(voiceVo.getSoundUrl());
        return voiceVo;
    }
}
